# ================================================
# 第一阶段：依赖安装阶段
# ================================================
FROM python:3.9-slim AS builder

WORKDIR /build

# 只配置华为云镜像源
RUN rm -f /etc/apt/sources.list && \
    find /etc/apt/sources.list.d/ -type f -delete && \
    echo "deb https://repo.huaweicloud.com/debian/ bookworm main contrib non-free" > /etc/apt/sources.list && \
    echo "deb https://repo.huaweicloud.com/debian/ bookworm-updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb https://repo.huaweicloud.com/debian-security bookworm-security main contrib non-free" >> /etc/apt/sources.list

# 安装构建工具(使用BuildKit缓存apt包)
ENV DEBIAN_FRONTEND=noninteractive
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update -o Acquire::https::Verify-Peer=false -yq && \
    apt-get install -y --no-install-recommends \
    build-essential \
    curl \
    && apt-get clean


# 配置pip
# 设置环境变量，指定 PyPI 镜像源为阿里云
ENV PIP_INDEX_URL https://mirrors.aliyun.com/pypi/simple
RUN pip install --upgrade pip

# 复制依赖文件
COPY requirements_server.txt .

# 构建wheel包(使用BuildKit缓存pip)
ENV PIP_DEFAULT_TIMEOUT=600
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked \
    pip wheel --wheel-dir /wheels -r requirements_server.txt && \
    echo "依赖包构建完成"




# ================================================
# 第二阶段：运行阶段
# ================================================
FROM python:3.9-slim

WORKDIR /app

# 创建funasr_user用户
RUN groupadd -r funasr_user && useradd -r -g funasr_user funasr_user

# 只配置华为云镜像源
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    rm -f /etc/apt/sources.list && \
    find /etc/apt/sources.list.d/ -type f -delete && \
    echo "deb https://repo.huaweicloud.com/debian/ bookworm main contrib non-free" > /etc/apt/sources.list && \
    echo "deb https://repo.huaweicloud.com/debian-security bookworm-security main contrib non-free" >> /etc/apt/sources.list && \
    apt-get update -o Acquire::https::Verify-Peer=false -yq && \
    apt-get install -y --no-install-recommends \
    curl \
    libgomp1 \
    ffmpeg \
    && apt-get clean

# 配置pip
# 设置环境变量，指定 PyPI 镜像源为阿里云
ENV PIP_INDEX_URL https://mirrors.aliyun.com/pypi/simple
RUN pip install --upgrade pip

# 从第一阶段复制构建好的wheel包
COPY --from=builder /wheels /wheels

# 安装依赖
ENV PIP_DEFAULT_TIMEOUT=600
RUN --mount=type=cache,target=/root/.cache/pip,sharing=locked \
    pip install --no-cache-dir /wheels/* && \
    rm -rf /wheels && \
    echo "依赖安装完成"

# 复制应用代码
COPY --chown=funasr_user:funasr_user . .

# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONIOENCODING=utf-8

# 创建模型缓存目录并设置权限
RUN mkdir -p /home/funasr_user/.cache/modelscope && \
    mkdir -p /home/funasr_user/.modelscope && \
    chown -R funasr_user:funasr_user /home/funasr_user/.cache && \
    chown -R funasr_user:funasr_user /home/funasr_user/.modelscope

# 暴露WebSocket端口
EXPOSE 10096

# 切换到funasr_user用户
USER funasr_user

# 启动服务
CMD ["python", "funasr/funasr_wss_server.py", "--port", "10096", "--host", "0.0.0.0", "--ngpu", "1", "--device", "mps", "--certfile", ""]




